package com.example.zsfinacialmanage.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.constant.MessageConstant;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import com.example.zsfinacialmanage.utils.Md5Util;

public class LoginActivity extends AppCompatActivity {

    //定义对象
    private EditText et_name,et_pwd;
    private Button btn_newRegister,btn_login,btn_forgot;
    private CheckBox check_reme;
    private MyDBHelper myDBHelper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //绑定控件
        initView();
        //登录按钮功能实现
        btnLogin();
        //新用户注册按钮功能实现
        btnNewRegister();
        //忘记密码按钮功能实现
        btnForgot();
    }

    //登录按钮功能实现
    private void btnLogin() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //1.获取用户输入的账号和密码
                String inputName = et_name.getText().toString();
                String inputPwd = et_pwd.getText().toString();
                String inputPwdMd5 = Md5Util.get(inputPwd);

                //创建一个editor后面保存用户名、密码和复选框状态
                SharedPreferences.Editor editor = getSharedPreferences("userinfo",0).edit();

                //2.判断是否为空
                if (inputName.equals("") || inputPwd.equals("")){
                    //用户名或密码为空的情况
                    Toast.makeText(LoginActivity.this, MessageConstant.ACCOUNT_OR_PASSWORD_CANNOT_NULL,Toast.LENGTH_SHORT).show();
                }else {
                    //账号密码不为空的情况，判断是否正确
                    //查询数据库
                    Cursor cursor = db.rawQuery("select * from tb_userinfo where name = ? and pwd = ?",new String[]{inputName,inputPwdMd5});
                    //根据查询结果进行判断
                    if (cursor.moveToNext()){
                        //查询到
                        @SuppressLint("Range") String getName = cursor.getString(cursor.getColumnIndex("name"));
                        @SuppressLint("Range") String getPwd = cursor.getString(cursor.getColumnIndex("pwd"));
                        if (inputName.equalsIgnoreCase(getName) && inputPwdMd5.equalsIgnoreCase(getPwd)){
                            editor.putString("userName",inputName);
                            editor.putString("userPwd",inputPwd);
                            editor.putString("userPwdMd5",inputPwdMd5);
                            editor.putBoolean("st",check_reme.isChecked());
                            editor.commit();
                            Toast.makeText(LoginActivity.this,MessageConstant.LOGIN_SUCCESS + "  欢迎回来！",Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }else {
                        //未查询到
                        Toast.makeText(LoginActivity.this,MessageConstant.USER_NOT_EXISTS,Toast.LENGTH_SHORT).show();
                        et_name.setText("");
                        et_pwd.setText("");
                    }
                }
            }
        });
        //记住密码复选框功能设置
        String myName = getSharedPreferences("userinfo",0).getString("userName","");
        String myPwd = getSharedPreferences("userinfo",0).getString("userPwd","");
        Boolean mySt = getSharedPreferences("userinfo",0).getBoolean("st",false);
        if (mySt == true){
            et_name.setText(myName);
            et_pwd.setText(myPwd);
            check_reme.setChecked(true);
        }else {
            et_name.setText("");
            et_pwd.setText("");
            check_reme.setChecked(false);
        }
    }

    private void btnForgot() {
        btn_forgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RecoverActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    //新用户注册按钮功能实现
    private void btnNewRegister() {
        btn_newRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void initView() {
        et_name = findViewById(R.id.et_name_lg);
        et_pwd = findViewById(R.id.et_pwd_lg);
        btn_login = findViewById(R.id.bt_login_lg);
        btn_newRegister = findViewById(R.id.bt_newregister_lg);
        btn_forgot = findViewById(R.id.bt_forgot_lg);
        check_reme = findViewById(R.id.cb_reme_lg);
        myDBHelper = new MyDBHelper(LoginActivity.this);
        db = myDBHelper.getWritableDatabase();
    }
}